package com.tim.source.templete;

import java.io.IOException;
import java.io.InputStream;

import com.tim.source.MakeSource;
import com.tim.source.common.CommonInfo;
import com.tim.source.common.TableInfo;
import com.tim.source.util.StringUtil;

/**
 * 数据表转hbm.xml.
 * 
 * @author Tim.
 * 
 */
public class TableToIService {
	// 接口类名
	public static String CLASS;
	// Service接口完整路径.
	public static String FULL_CLASS;
	// Service类文件模板.
	private static String TEMPLETE;
	static {
		try {
			// 获取IService模板.
			InputStream input = TableToIService.class
					.getResourceAsStream("iservice.tmp");
			byte[] data = new byte[input.available()];
			input.read(data);
			TEMPLETE = new String(data, "UTF-8");
			// 替换文件创建时间.
			TEMPLETE = TEMPLETE.replace("%dateTime%",
					CommonInfo.DATESTRING);
			// 替换文件创建作者.
			TEMPLETE = TEMPLETE.replace("%author%",
					CommonInfo.AUTHOR);
			input.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static String execute() {
		// IService路径.
		FULL_CLASS = MakeSource.SERVICE.replace("/", ".");
		// 替换package.
		if (FULL_CLASS.endsWith(".")) {
			FULL_CLASS = FULL_CLASS.substring(0, FULL_CLASS.lastIndexOf("."));
		}
		TEMPLETE = TEMPLETE.replace("%package%", FULL_CLASS);
		// 添加import Entity语句.
		String tempStr = TEMPLETE.replace("%importList%", StringUtil
				.getImportString(TableInfo.fullClassName));
		// 替换import.
		tempStr = tempStr.replace("%importList%", tempStr);
		tempStr = tempStr.replace("%className%", TableInfo.className);
		CLASS = "I" + TableInfo.className + "Service";
		// 设置Service接口路径
		FULL_CLASS = FULL_CLASS + "." + CLASS;
		return tempStr;
	}
}
